package com.base.core.framework.mybatis.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gitee.magic.core.annotations.SqlCondition.SqlConditionType;
import com.gitee.magic.core.utils.StringUtils;
import com.gitee.magic.framework.base.utils.MapConvert;
import com.gitee.magic.framework.base.utils.ParamValue;
import com.gitee.magic.jdbc.persistence.EntityObject;
import com.gitee.magic.jdbc.persistence.EntityProperty;
import com.gitee.magic.jdbc.persistence.source.jdbc.sqlplus.conditions.SqlWrapperBuilder;

/**
 * @author start
 */
public class SqlWrapperBuilderV2Ext extends SqlWrapperBuilder {

	public static final String DELETEDFIELD = "deleted";

	@SuppressWarnings("unchecked")
	public static <T,R> QueryWrapper<T> buildQueryWrapper(EntityObject eObject, Boolean idhandle, Class<R> rpro,
			Collection<ParamValue> params) {
		Map<String, String> pmapping = MapConvert.mapping(rpro);
		Map<String, String> emapping = new HashMap<>(100);
		for (EntityProperty member : eObject.getMembers()) {
			emapping.put(member.getFieldName(), member.getTableFieldName());
		}
		emapping.put("id", eObject.getEntityPropertyStubPrimaryKey().getTableFieldName());
		List<String> listSelect = new ArrayList<>();
		if (idhandle) {
			// 争对ID的特殊处理
			String idStr = "id";
			if (!pmapping.containsValue(idStr)) {
				String pid = eObject.getName() + "Id";
				if (pmapping.containsValue(pid) && !emapping.containsValue(pid)) {
					listSelect.add(eObject.getEntityPropertyStubPrimaryKey().getTableFieldName() + " as " + pid);
				}
			}
		}
		for (String e : pmapping.keySet()) {
			if (emapping.containsKey(e)) {
				listSelect.add(emapping.get(e) + " as " + pmapping.get(e));
			}
		}
		QueryWrapper<T> wrapper = new QueryWrapper<>();
		wrapper.select(listSelect.toArray(new String[listSelect.size()]));
		for (ParamValue paramValue : params) {
			if (emapping.containsValue(paramValue.getColumnName())) {
				String columnName=paramValue.getAliasColumnName();
				for(SqlConditionType condition:paramValue.getCondition()) {
					if (condition == SqlConditionType.IN) {
						wrapper.in(!StringUtils.isEmpty(paramValue.getValue()), columnName, (Collection<?>)paramValue.getValue());
					} else if (condition == SqlConditionType.NOT) {
						wrapper.notIn(!StringUtils.isEmpty(paramValue.getValue()), columnName, (Collection<?>)paramValue.getValue());
					} else if (condition == SqlConditionType.LIKE) {
						wrapper.like(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.LIKE_LEFT) {
						wrapper.likeLeft(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.LIKE_RIGHT) {
						wrapper.likeRight(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.EQ) {
						wrapper.eq(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.NE) {
						wrapper.ne(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.GT) {
						wrapper.gt(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.GE) {
						wrapper.ge(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.LT) {
						wrapper.lt(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.LE) {
						wrapper.le(!StringUtils.isEmpty(paramValue.getValue()), columnName, paramValue.getValue());
					} else if (condition == SqlConditionType.IS_NULL) {
						wrapper.isNull(columnName);
					} else if (condition == SqlConditionType.IS_NOT_NULL) {
						wrapper.isNotNull(columnName);
					}
				}
			}else {
				for(SqlConditionType condition:paramValue.getCondition()) {
					if(paramValue.getValue()!=null) {
						if (condition == SqlConditionType.GROUP_BY||
								condition == SqlConditionType.ASC||
								condition == SqlConditionType.DESC) {
							if(List.class.isAssignableFrom(paramValue.getValue().getClass())) {
								List<String> data=(List<String>)paramValue.getValue();
								if (condition == SqlConditionType.GROUP_BY) {
									wrapper.groupBy(data);
								} else if (condition == SqlConditionType.ASC) {
									wrapper.orderByAsc(data);
								} else if (condition == SqlConditionType.DESC) {
									wrapper.orderByDesc(data);
								}
							}else if(paramValue.getValue().getClass().isArray()) {
								String[] paramVales=(String[])paramValue.getValue();
								if (condition == SqlConditionType.GROUP_BY) {
									wrapper.groupBy(Arrays.asList(paramVales));
								} else if (condition == SqlConditionType.ASC) {
									wrapper.orderByAsc(Arrays.asList(paramVales));
								} else if (condition == SqlConditionType.DESC) {
									wrapper.orderByDesc(Arrays.asList(paramVales));
								}
							}else {
								String paramVales=String.valueOf(paramValue.getValue());
								if (condition == SqlConditionType.GROUP_BY) {
									wrapper.groupBy(paramVales);
								} else if (condition == SqlConditionType.ASC) {
									wrapper.orderByAsc(paramVales);
								} else if (condition == SqlConditionType.DESC) {
									wrapper.orderByDesc(paramVales);
								}
							}
						}
					}
				}
			}
		}
		return wrapper;
	}

}

